package com.orange.core.service.venue.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.service.venue.VenueService;
import com.orange.core.mapper.venue.VenueMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.venue.dto.VenueDTO;
import com.orange.core.pojo.entity.venue.vo.VenueVO;
import com.orange.core.pojo.entity.venue.Venue;
import com.orange.core.pojo.req.venue.VenuePageReq;
import com.orange.core.util.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * <p>ClassName:VenueService</p>
 * <p>Description: 场地管理service</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/5/22 0:18</p>
 */
@Service
public class VenueServiceImpl extends ServiceImpl<VenueMapper,Venue>  implements VenueService{


    @Override
    public Page<VenueVO> page(PageReqDto<VenuePageReq> pageReqDto) {

        String name = Optional.ofNullable(pageReqDto.getCondition()).map(VenuePageReq::getName).orElse(null);

        Page<Venue> page = this.lambdaQuery().like(name != null, Venue::getName, name)
                .orderByDesc(Venue::getId)
                .page(pageReqDto.toPage());

        Page<VenueVO> venueVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(), page.getTotal());
        List<VenueVO> venueVOList = ConvertUtils.convertListToVo(page.getRecords(), VenueVO.class);
        venueVOPage.setRecords(venueVOList);
        return venueVOPage;
    }

    @Override
    public VenueVO info(int id) {
        Venue venue = this.getById(id);
        Assert.notNull(venue, "场地不存在");
        VenueVO venueVO = BeanUtil.copyProperties(venue, VenueVO.class);
        return venueVO;
    }

    @Override
    @Transactional
    public boolean save(VenueDTO venueDTO) {
        Venue venue = new Venue();
        BeanUtil.copyProperties(venueDTO, venue);
        boolean result = super.save(venue);
        return result;
    }

    @Override
    @Transactional
    public boolean update(VenueDTO venueDTO) {
        Venue venue = new Venue();
        BeanUtil.copyProperties(venueDTO, venue);
        boolean result = super.updateById(venue);
        return result;
    }

    @Override
    @Transactional
    public Boolean deleteVenueByIds(Integer[] ids) {
        boolean result = this.removeBatchByIds(Arrays.asList(ids));
        return result;
    }

}
